package com.example.wisdomclassroomapi.common.util;

import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;


import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Slf4j
public class MsgUtils {

    @Resource
    private  RedisTemplate<String, String> redisTemplate;
    /**
     * 生成6位随机数验证码
     * @return
     */
    public static String vcode(){
        String vcode = "";
        for (int i = 0; i < 6; i++) {
            vcode = vcode + (int)(Math.random() * 9);
        }
        return vcode;
    }


    public   boolean sendCode(String mobile){
        String key = "msgcode"+mobile;
        String code=vcode();
        log.info("value of msgcode:mobile={},code= {}",mobile,code);
        redisTemplate.opsForValue().set(key, vcode());
        redisTemplate.expire(key, 60, TimeUnit.SECONDS);
        return true;
    }

    public static boolean verifyCode(String clientId,String code){
//        String key = "msgcode"+mobile;
//        log.info("verify Code:mobile={},code= {}",mobile,code);
//        String code1=redisTemplate.opsForValue().get(key);
//        if(StringUtils.equals(code,code1)){
//            return true;
//        }
//        return false;
        return true;
    }
}
